package vue;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;
import modele.Jeu1;

public class VueControleur extends JPanel {


    final CardLayout cl = new CardLayout();
    JPanel content = new JPanel();
    String[] listContent = {"acceuil", "jeux", "param","scores"};
    int indice = 0;

    public VueControleur() {
        creerFenetre();
    }

    private void creerFenetre() {


        /*Panneau d'acceuil*/
        JPanel card1 = new PanneauBox();
        JButton jeux = new JButton("Lancer jeu");
        JButton scores = new JButton("Meilleurs scores");
        JButton param = new JButton("Parametres");
        JButton quitter = new JButton("Quitter");
        jeux.setAlignmentX(CENTER_ALIGNMENT);
        scores.setAlignmentX(CENTER_ALIGNMENT);
        param.setAlignmentX(CENTER_ALIGNMENT);
        quitter.setAlignmentX(CENTER_ALIGNMENT);
        card1.add(jeux);
        card1.add(scores);
        card1.add(param);
        card1.add(quitter);

        /* Choix des jeux */
        JPanel card2 = new PanneauBox();
        JButton retour = new JButton("Retour");
        JButton jeu1 = new JButton("Jeu1");
        JButton jeu2 = new JButton("Jeu2");
        jeu1.setAlignmentX(CENTER_ALIGNMENT);
        jeu2.setAlignmentX(CENTER_ALIGNMENT);
        retour.setAlignmentX(CENTER_ALIGNMENT);
        card2.add(jeu1);
        card2.add(jeu2);
        card2.add(retour);
        
        /*Parametres */
        JPanel card3 = new PanneauBox();
        JButton retour2 = new JButton("Retour");
        JButton duree1 = new JButton("30 secondes");
        JButton duree2 = new JButton("60 secondes");
        duree1.setAlignmentX(CENTER_ALIGNMENT);
        duree2.setAlignmentX(CENTER_ALIGNMENT);
        retour2.setAlignmentX(CENTER_ALIGNMENT);
        card3.add(duree1);
        card3.add(duree2);
        card3.add(retour2);
        
         /*Meilleurs scores */
        JPanel card4 = new PanneauBox();
        JButton retour3 = new JButton("Retour");
        retour3.setAlignmentX(CENTER_ALIGNMENT);
        card4.add(retour3);
        
        
        this.add(content);
        content.setLayout(cl);
        content.add(card1, listContent[0]);
        content.add(card2, listContent[1]);
        content.add(card3, listContent[2]);
        content.add(card4, listContent[3]);
        
        
        

        jeux.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VueControleur.this.cl.show(content, "jeux");
            }
        });

        param.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VueControleur.this.cl.show(content, "param");
            }
        });
        
        retour.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VueControleur.this.cl.show(content, "acceuil");
            }
        });
        
        retour2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VueControleur.this.cl.show(content, "acceuil");
            }
        });
        
        retour3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                VueControleur.this.cl.show(content, "acceuil");
            }
        });
        
        jeu1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    VueControleur.this.lancerjeu();
                } catch (SQLException ex) {
                    Logger.getLogger(VueControleur.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        scores.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               VueControleur.this.cl.show(content, "scores");
            }
        });
        
        quitter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

    }
    
    
    public void lancerjeu() throws SQLException{
        this.removeAll();
        Jeu1 j1 =new Jeu1(5);
        VueJeu1 v = new VueJeu1();
        j1.setVue(v);
      
        this.add(v);
        this.repaint();
        this.revalidate();
        this.getParent().repaint();
        this.getParent().revalidate();
        j1.run();
        
        
    }

}
